﻿using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
using Polly;
using NetMicro.Core.Helper;
using System.Net;

namespace NetMicro.HttpClient.Polly
{
    public class PolicyHandler : DelegatingHandler
    {
        protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
        {
            string key = $"{request.Method}-{request.RequestUri.AbsolutePath}";
            var policis = PollyQueue.Get(key);
            if (policis != null)
            {
                return policis.ExecuteAsync(ct => base.SendAsync(request, ct), cancellationToken);
            }
            return base.SendAsync(request, cancellationToken);
        }
    }

}
